% t: time step
% T1: team inforamtion
% S1: partial state of player 1, or full team state
% S2--S5: partial states of players 2--5

function A=TeamAgent(t,T1,S1,S2,S3,S4,S5)

global possession;

global system_mode;
global num_players_r;
global learning_algorithm;
global policy;

global record_states;
global state_set;

global Fa;
global Qa;

if(TeamInPossession(T1(1,:),S1,'f'))
    possession = possession + 1;
end

if strcmp(system_mode,'learn') || strcmp(system_mode,'load')
    %'LEARN'
    if (t == 1)  
        for k = 1:num_players_r
            P = T1(k,:);
            P_num = P{3};
            
            A{k} = Random(P,S1,'f');
            
            %load the selected action
            memory = LoadMemory(P);
            action = memory(2);
            
            %update Fa
            Fa{P_num} = GetFeatures(P,S1,'f', action);
        end
    else
        for k = 1:num_players_r
            A{k} = Agent(T1(k,:),S1,'f', learning_algorithm, false, 0);
        end
    end

elseif strcmp(system_mode,'follow')
    %'FOLLOW'
    
    if ( t == 1 && record_states)
        index = numel(state_set) + 1;
        state_set{index} = S1;
    end
    % do the actual simulation
    for k = 1:num_players_r
        A{k} = Agent(T1(k,:),S1,'f', @SelectActionFromPolicy, true, policy);
    end

else
    %'RANDOM'
    %A = {Random(T1(1,:),S1,'f'), '', '', '',''};
%     for k = 1:num_players_r
%         A{k} = Random(T1(k,:),S1,'f');
%     end
%     for k = 1:num_players_r
%         A{k} = DefaultStrategy(T1(k,:),S1,'f');
%     end
    for k = 1:num_players_r
        A{k} = ImprovedHeuristic(T1(k,:),S1,'f');
    end
end    

end